﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DemoObject;
using BusinessObjects;
using HTT.Utility;

public partial class TypeCodeAE : System.Web.UI.UserControl
{
    public Guid TypeCodeId { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["TypeCodeId"] != null)
        {
            TypeCodeId = new Guid(Request["TypeCodeId"].ToString());
            btnUpdate.Text = "Cập nhật";
        }
        else TypeCodeId = Guid.Empty;
        if (!IsPostBack)
        {
            LoadData();
        }
    }
    private void LoadData()
    {
        if (!TypeCodeId.Equals(Guid.Empty))
        {
            TblTypeCode item = TblTypeCodeRepository.CreateInstant().SelectOne(TypeCodeId);
            if (item != null)
            {
                txtName.Text = item.Name;
                txtValue.Text = item.Value;
                lblCreatedBy.Text = item.CreatedBy;
                lblCreatedDate.Text = item.CreatedDate.ToString("dd/MM/yyyy");
                lblUpdatedBy.Text = item.UpdatedBy;
                lblUpdatedDate.Text = item.UpdatedDate.ToString("dd/MM/yyyy");
            }
        }
    }
    private TblTypeCode GetTblTypeCode()
    {
        TblTypeCode obj = TblTypeCodeRepository.CreateInstant().SelectOne(TypeCodeId);
        if (obj == null)
        {
            obj = new TblTypeCode();
            obj.Id = Guid.NewGuid();
            obj.CreatedBy = HttpContext.Current.User.Identity.Name;
            obj.CreatedDate = DateTime.Now;
        }
        obj.Name = txtName.Text;
        obj.Value = txtValue.Text;
        obj.UpdatedBy = HttpContext.Current.User.Identity.Name;
        obj.UpdatedDate = DateTime.Now;

        return obj;
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (TypeCodeId.Equals(Guid.Empty))
            TblTypeCodeRepository.CreateInstant().Insert(GetTblTypeCode());
        else
            TblTypeCodeRepository.CreateInstant().Update(GetTblTypeCode());
        Response.Redirect("TypeCodeList.aspx");
    }


}
